define(function(require, exports){
	var $ = require('jquery');
	var pubjs = require('pubjs');
	var view  = require('@base/view');
	var common = require('modules/common');
	var util = require('util');

	// 创意包上传子模块
	var SubCreative = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'fileId': 0,
				'data': null,
				'class': 'uk-form M-uploaderCreative',
				'width': 0, // 限制的宽
				'height': 0, // 限制的高
				'defaultImage': window.ROOT('resources/images/default.png')
			});
			this.$data = null;
			this.$fileId = config.get('fileId');
			this.Super('init', arguments);
		},
		afterBuild: function(){
			var c = this.getConfig();
			var el = this.getDOM();
			$([
				'<li class="" data-id=' + c.fileId +'>',
					'<div class="M-uploaderCreativeLayoutColumn M-uploaderCreativeThumb"><img alt="'+LANG('缩略图')+'" src="'+c.defaultImage+'"/></div>',
					'<div class="M-uploaderCreativeLayoutColumn">',
						'<div pub-name="name" pub-mod="@base/common/input.text" class="M-uploaderCreativeName"/>',
						'<div class="M-uploaderCreativeInfo">',
							'<label>'+LANG('类型：')+'</label><span class="M-uploaderCreativeType mr20" />',
							'<label>'+LANG('大小：')+'</label><span class="M-uploaderCreativeSize" />',
						'</div>',
						'<div><label>'+LANG('尺寸：')+'</label><span class="M-uploaderCreativeShape"/></div>',
					'</div>',
					'<div class="M-uploaderCreativeLayoutColumn status" />',
					'<div class="M-uploaderCreativeLayoutColumn" pub-name="assist" pub-mod="modules/upload.assist" />',
					'</div>',
				'</li>'
			].join('')).appendTo(el);

			this.$doms = {
				type: el.find('.M-uploaderCreativeType'),
				size: el.find('.M-uploaderCreativeSize'),
				thumb: el.find('.M-uploaderCreativeThumb img'),
				shape: el.find('.M-uploaderCreativeShape')
			};

			var config = {
				assist: {
					hasDelete: true,
					hasPreview: true,
					target: el.find('.status'),
					type: 'sweetyCreative'
				},
				name: {
					value: '',
					width: 300,
					events: 'blur'
				}
			};

			this.buildFromTemplate(config);
		},
		afterBuildTemplate: function(){
			var data = this.getConfig('data');
			if(data){
				this.setData(data);
				this.$.assist.update(true);
			}
		},
		onInputBlur: function(ev){
			if(this.$data){
				this.$data.name = ev.param.value;
			}
			return false;
		},
		onFileUploaded: function(ev){
			var file = ev.param[0];
			var data = ev.param[1];
			var c = this.getConfig();

			if(this.$fileId == file.id){
				var size_error, duration_error;
				if(c.width && c.width !== data.width)
				{
					size_error = true;
				}
				if(c.height && c.height !== data.height)
				{
					size_error = true;
				}
				if(c.duration && c.duration !== data.duration)
				{
					duration_error = true;
				}

				this.showError && this.showError({size: size_error, duration: duration_error});
				this.$.assist.update(true);
				this.setData(data, true);
				this.fire('uploadSuccess', file.id);
			}
		},
		//todo 尝试将isEdit去掉
		setData: function(data, isEdit){
			var doms = this.$doms;

			if(isEdit){
				this.$.assist.update(true);
			}
			var id = data.id;
			data.id = isEdit ? 0 : id;
			data.fileId = isEdit ? id : 0;

			this.$data = data;

			if(data.thumb) {
				var thumb = data.thumb + '&w=65&h=65';
				common.loadImage('creative', thumb, doms.thumb);
			}

			if(this.$.name)
			{
				this.$.name.setValue(data.name||'');
			}
			doms.type.text(data.type||'');
			var size = data.size || '';
			if(data.duration)
			{
				size += `;${LANG('时长: %1 秒', data.duration)}`;
				if(doms.duration)
				{
					doms.duration.text(data.duration + LANG('秒'));
				}
			}

			doms.size.text(size);
			doms.shape.text((data.width||'')+'*'+(data.height||''));

			// 设置预览地址
			this.$.assist.setData({id: (isEdit ? 'fid_'+data.fileId : data.id)}).setPreview(data.previewUrl);
		},
		getValue: function(){
			var data = this.$data;
			return $.extend({}, data, {
				creativeId: data.id,
				type: 'upload'
			});
		},
		// 上传错误时，去掉此模块。
		onUploadError: function(ev) {
			var id = ev.param && ev.param.file && ev.param.file.id;
			if(id === this.$data.id) {
				this.destroy();
			}
		},
		onChildDeleted: function(){
			var self = this;
			this.fire('uploaderItemDeleted', this.$data, function(){
				self.destroy();
			});
		}
	});

	exports.base = SubCreative;

	// 创意包上传子模块
	var PicTextCreative = SubCreative.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'fileId': 0,
				'data': null,
				'class': 'uk-form M-uploaderPTC',
				'defaultImage': window.ROOT('resources/images/default.png'),
				'assist_conf': {
					hasDelete: false,
					hasPreview: false,
					hasDone: false,
					type: 'sweetyPicText'
				},
				'view_model': {
					'errorView': false
					,'errorSize': false
					,'errorDuration': false
				}
			});
			this.$data = null;
			this.$fileId = config.get('fileId');
			this.Super('init', arguments);
		},
		afterBuild: function(){
			var c = this.getConfig();
			var el = this.getDOM();
			$([
				'<li class="" data-id=' + c.fileId +'>',
				'<div class="M-uploaderPTCLayout M-uploaderCreativeThumb cur_p"><img alt="'+LANG('缩略图')+'" src="'+c.defaultImage+'"/></div>',
				'<i class="M-uploaderPTCClose icon-close"></i>',
				'<div class="M-uploaderPTCLayoutColumn" ms-class="dn: errorView">',
					'<div class="M-uploaderCreativeInfo">',
						'<label>'+LANG('类型：')+'</label><span class="M-uploaderCreativeType mr20" />',
					'</div>',
					'<div class="M-uploaderCreativeInfo">',
						'<label>'+LANG('大小：')+'</label><span class="M-uploaderCreativeSize mr20" />',
					'</div>',
					'<div class="M-uploaderCreativeInfo ">',
						'<label>'+LANG('尺寸：')+'</label><span class="M-uploaderCreativeShape"/>',
					'</div>',
				'</div>',
				'<div class="M-uploaderPTCLayoutColumn">',
					'<div class="M-uploaderCreativeInfo uk-text-danger" ms-class="dn: !errorSize">',
						'<label>'+LANG('尺寸错误:')+'</label><span class="M-uploaderCreativeShape"/>',
					'</div>',
					'<div class="M-uploaderCreativeInfo uk-text-danger" ms-class="dn: !errorDuration">',
						'<label>'+LANG('时长错误:')+'</label><span class="M-uploaderCreativeDuration"/>',
					'</div>',
				'</div>',
				'<div class="M-uploaderCreativeLayoutColumn status" />',
				'<div class="M-uploaderCreativeLayoutColumn" pub-name="assist" pub-mod="modules/upload.assist" />',
				'</div>',
				'</li>'
			].join('')).appendTo(el);

			this.$doms = {
				type: el.find('.M-uploaderCreativeType'),
				size: el.find('.M-uploaderCreativeSize'),
				thumb: el.find('.M-uploaderCreativeThumb img'),
				shape: el.find('.M-uploaderCreativeShape'),
				duration: el.find('.M-uploaderCreativeDuration')
			};

			this.uiBind(el.find('.M-uploaderCreativeThumb'), 'click', 'eventPreview');
			this.uiBind(el.find('.M-uploaderPTCClose'), 'click', 'eventClose');

			var config = {
				assist: util.extend({
					target: el.find('.status')
				}, c.assist_conf),
				name: {
					value: '',
					width: 300,
					events: 'blur'
				}
			};

			this.buildFromTemplate(config);
		},
		showError: function(param)
		{
			if(param && param.size)
			{
				this.vm.$.errorSize = true;
			}
			if(param && param.duration)
			{
				this.vm.$.errorDuration = true;
			}

			this.vm.$.errorView = this.vm.$.errorDuration || this.vm.$.errorSize;
		},
		eventPreview: function(ev, elm) {
			this.$.assist.showPreview();
/*			if(this.$previewUrl){
				window.open(this.$previewUrl);
			}else{
				window.open(this.$data.thumb);
			}*/
		},
		eventClose: function()
		{
			this.onChildDeleted();
		}
	});

	exports.picText = PicTextCreative;
});